<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <!-- 给头部组件传参方法 -->
        <MyHeader :handlerAdd="handlerAdd" />
        <!-- 给中间组件传参和传参方法 -->
        <MyList
          :todos="todos"
          :deleteTodo="deleteTodo"
          :checkTodo="checkTodo"
        />
        <!-- 给底部组件传参 -->
        <MyFooter
          :todos="todos"
          :checkAllTodo="checkAllTodo"
          :clearCheckedTodo="clearCheckedTodo"
        />
      </div>
    </div>
  </div>
</template>

<script>
// 导入的头部的组件
import MyHeader from "./components/MyHeader.vue";
// 导入的底部的组件
import MyFooter from "./components/MyFooter.vue";
// 导入中间的组件
import MyList from "./components/MyList.vue";
export default {
  name: "App",
  // 创建组件
  components: {
    MyHeader,
    MyFooter,
    MyList,
  },
  // 创建数据
  data() {
    return {
      todos: [
        { id: "001", title: "吃饭", done: true },
        { id: "002", title: "睡觉", done: false },
        { id: "003", title: "开车", done: true },
        { id: "004", title: "抽烟", done: false },
      ],
    };
  },
  methods: {
    // 数据添加的逻辑
    handlerAdd(value) {
      // 将收到的数据添加到todo里面去
      this.todos.unshift(value);
    },
    // 数据删除的逻辑
    deleteTodo(id) {
      this.todos = this.todos.filter((todo) => todo.id !== id);
    },
    // 数据勾选和数据不勾选的逻辑
    checkTodo(id) {
      this.todos.forEach((todo) => {
        todo.id === id ? (todo.done = !todo.done) : undefined;
      });
    },
    // 数据全选或全不选的逻辑
    checkAllTodo(done) {
      this.todos.forEach((todo) => {
        todo.done === done;
      });
    },
    // 清除已经完成的逻辑
    clearCheckedTodo() {
      this.todos = this.todos.filter((item) => !item.done === true);
    },
  },
};
</script>


<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

/*app*/
.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>